import requests
import chardet
import hashlib
import os
import re
from urllib.parse import urlparse


def get_webpage_title(url):
    s = requests.session()
    try:
        html = requests.get(url, timeout=5).content
    except Exception as e:
        return url
    charset = chardet.detect(html)['encoding']
    result = s.get(url)
    if charset == "GB2312" or charset is None:
        result.encoding = 'gbk'
    else:
        result.encoding = 'utf-8'
    content = result.text
    title = re.findall('<title>(.*)</title>', content)
    if len(title) > 0:
        return title[0]
    else:
        urls = url.split("/")
        return urls.pop()


def hash_str(text):
    return hashlib.md5(text.encode(encoding="utf8")).hexdigest()


def save_file(path, data):
    f = open(path, "wb")
    f.write(data)
    f.close()


def get_base_url(url):
    parse = urlparse(url)
    return parse.scheme + "://" + parse.netloc


def path_to_url(path):
    if in_directory(path, "./data"):
        return path.replace('./', "/")
    else:
        return "/data/index.html"


def get_page_icon(url):
    icon_url = get_base_url(url) + "/favicon.ico"
    icon_url_md5 = hash_str(icon_url)
    icon_path_dir = "./data/icon/" + icon_url_md5 + "/"
    icon_path = icon_path_dir + "favicon.ico"

    if os.path.exists(icon_path):
        return path_to_url(icon_path)
    try:
        response = requests.get(icon_url, timeout=5)
    except Exception as e:
        return "/static/img/favicon.ico"
    if response.status_code == 200:
        os.makedirs(icon_path_dir)
        save_file(icon_path, response.content)
        return path_to_url(icon_path)
    else:
        return "/static/img/favicon.ico"


def in_directory(file, directory):
    directory = os.path.join(os.path.realpath(directory), '')
    file = os.path.realpath(file)
    return os.path.commonprefix([file, directory]) == directory


